package braden.cis.uab.edu.automaton;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import braden.cis.uab.edu.R;

public class State {

	private ArrayList<Transition> transitions = new ArrayList<Transition>();
	private Bitmap bitmap;
	private int x, y;
	private int label;
	private boolean isInitial, isFinal;
	private Context context;
	
	public State(Context context, int label) {
		this.context = context;
		this.bitmap = BitmapFactory.decodeResource(context
				.getResources(), R.drawable.yellow_state);
		x = 100;
		y = 100;
		this.label = label;
	}

	public boolean isInitial() {
		return isInitial;
	}

	public void setInitial(boolean isInitial) {
		if (isInitial) {
			if (this.isFinal) {
				this.setGraphic(BitmapFactory.decodeResource(context
						.getResources(), R.drawable.initial_final_state));
			} else {
				this.setGraphic(BitmapFactory.decodeResource(context
						.getResources(), R.drawable.initial_state));
			}
		} else {
			if (this.isFinal) {
				this.setGraphic(BitmapFactory.decodeResource(context
						.getResources(), R.drawable.final_state));
			} else {
				this.setGraphic(BitmapFactory.decodeResource(context
						.getResources(), R.drawable.yellow_state));
			}
		}
		this.isInitial = isInitial;
	}

	public boolean isFinal() {
		return isFinal;
	}

	public void setFinal(boolean isFinal) {
		if (isFinal) {
			if (this.isInitial) {
				this.setGraphic(BitmapFactory.decodeResource(context
						.getResources(), R.drawable.initial_final_state));
			} else {
				this.setGraphic(BitmapFactory.decodeResource(context
						.getResources(), R.drawable.final_state));
			}
		} else {
			if (this.isInitial) {
				this.setGraphic(BitmapFactory.decodeResource(context
						.getResources(), R.drawable.initial_state));
			} else {
				this.setGraphic(BitmapFactory.decodeResource(context
						.getResources(), R.drawable.yellow_state));
			}
		}
		this.isFinal = isFinal;
	}

	public int getLabel() {
		return label;
	}

	public void setLabel(int label) {
		this.label = label;
	}

	public Bitmap getGraphic() {
		return bitmap;
	}

	private void setGraphic(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	public int getX() {
		return x + bitmap.getWidth() / 2;
	}

	public void setX(int value) {
		x = value - bitmap.getWidth() / 2;
	}

	public int getY() {
		return y + bitmap.getHeight() / 2;
	}

	public void setY(int value) {
		y = value - bitmap.getHeight() / 2;
	}

	public ArrayList<Transition> getTransitions() {
		return transitions;
	}

	public void addTransition(Transition transition) {
		transitions.add(transition);
	}
	
	public void removeTransition(Transition transition) {
		transitions.remove(transition);
	}
}
